﻿using MCUPackage;
using MCUPackage.DesignEditor;
using MCUPackage.Utils;
using System;
using System.ComponentModel;

namespace SxPackages.STC8.Models
{
	[Serializable]
	class USART2Options : USARTOptionBase
	{
		private string mPins = "P1.0,P1.1";
		[DisplayName("使用引脚"),
		DefaultValue("P1.0,P1.1"),
		Description("本串口使用的引脚设置"),
		TypeConverter(typeof(StringCollectionConvertor)),
		MultiCollection(new string[] { "P1.0,P1.1" })]
		public string Pins
		{
			get => mPins;
			set
			{
				if (value != mPins)
				{
					if (SwitchPins(value, mPins))
					{
						mPins = value;
					}
				}
			}
		}

		protected override void SetPins(String[] aPins)
		{
			var opt = mParent.SetPinPurpose(aPins[0], PinPurpose.USART2_RCV);
			opt.Mode = GPIOMode.Standard;
			opt.Speed = GPIOSpeed.Fast;
			opt.EnableUp = true;
			opt = mParent.SetPinPurpose(aPins[1], PinPurpose.USART2_SND);
			opt.Mode = GPIOMode.Push;
			opt.Speed = GPIOSpeed.Fast;
		}

		public USART2Options(STC8HUSOP20 aParent) : base(aParent)
		{
			Id = 2;
			mUsePins = TemplateHelper.GetPins(mPins);
		}
	}
}
